package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.MmrsT;
import cn.e4j.modular.crm.mapper.MmrsTMapper;
import cn.e4j.modular.crm.service.MmrsTService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mengyb
 * @since 2019-04-18
 */
@Service
public class MmrsTServiceImpl extends ServiceImpl<MmrsTMapper, MmrsT> implements MmrsTService {

    @Transactional(readOnly = true)
    @Override
    public List<Map<String, Object>> getList(Dto dto, Page<Object> page) {
        ShiroUser shiroUser = ShiroKit.getUser();

        dto.put("ent", shiroUser.getDBC());
        dto.put("lang", LangUtils.getLang(shiroUser.getLANG()));

//        return this.baseMapper.getList(dto, page);
        return InvokeUtil.invokeBeanMethod("mmrsTMapper,getList",dto,page);
    }
    @Transactional(readOnly = true)
    @Override
    public List<Map<String, Object>> getGwamPageList(Dto dto, Page<Object> page) {
        ShiroUser shiroUser = ShiroKit.getUser();

        dto.put("ent", shiroUser.getDBC());
        dto.put("lang", LangUtils.getLang(shiroUser.getLANG()));

        return this.baseMapper.getGwamList(dto,page);
    }
    @Transactional(readOnly = true)
    @Override
    public List<Map<String, Object>> getGwamList(Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();

        dto.put("ent", shiroUser.getDBC());
        dto.put("lang", LangUtils.getLang(shiroUser.getLANG()));

        return this.baseMapper.getGwamList(dto);
    }
    @Transactional(readOnly = true)
    @Override
    public List<Map<String, Object>> getGwatPageList(Dto dto, Page<Object> page) {
        ShiroUser shiroUser = ShiroKit.getUser();

        dto.put("ent", shiroUser.getDBC());
        dto.put("lang", LangUtils.getLang(shiroUser.getLANG()));

        return this.baseMapper.getGwatList(dto, page);
    }
    @Transactional(readOnly = true)
    @Override
    public List<Map<String, Object>> getGwatList(Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();

        dto.put("ent", shiroUser.getDBC());
        dto.put("lang", LangUtils.getLang(shiroUser.getLANG()));

        return this.baseMapper.getGwatList(dto);
    }
    @Transactional(readOnly = true)
    @Override
    public List<Map<String, Object>> getUnitList(Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();

        dto.put("ent", shiroUser.getDBC());
        dto.put("lang", LangUtils.getLang(shiroUser.getLANG()));

        return this.baseMapper.getUnitList(dto);
    }


    @Transactional
    @Override
    public void add(MmrsT mmrsT) {
        ShiroUser shiroUser = ShiroKit.getUser();

        mmrsT.setMMRSENT(shiroUser.getDBC());
        mmrsT.setMMRSUSER(shiroUser.getACCOUNT());
        mmrsT.setMMRSGRUP(shiroUser.getORG_ID());
        mmrsT.setMMRSORIU(shiroUser.getACCOUNT());
        mmrsT.setMMRSORIG(shiroUser.getORG_ID());
        mmrsT.setMMRSTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
        mmrsT.setMMRSDATE(LocalDate.now());


        mmrsT.insert();

    }

    @Transactional
    @Override
    public void edit(MmrsT mmrsT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        mmrsT.setMMRSMODU(shiroUser.getACCOUNT());

        UpdateWrapper<MmrsT> mmrsTUpdateWrapper = new UpdateWrapper<>();

        mmrsTUpdateWrapper.eq("MMRSENT", mmrsT.getMMRSENT());
        mmrsTUpdateWrapper.eq("MMRS001", mmrsT.getMMRS001());
        mmrsTUpdateWrapper.eq("MMRS002", mmrsT.getMMRS002());
        mmrsTUpdateWrapper.eq("MMRS003", mmrsT.getMMRS003());
        mmrsTUpdateWrapper.eq("MMRS004", mmrsT.getMMRS004());
        mmrsTUpdateWrapper.eq("MMRS005", mmrsT.getMMRS005());
        mmrsTUpdateWrapper.eq("MMRS006", mmrsT.getMMRS006());

        mmrsT.update(mmrsTUpdateWrapper);
    }

    @Transactional
    @Override
    public void delRow(MmrsT mmrsT) {


        UpdateWrapper<MmrsT> mmrsTUpdateWrapper = new UpdateWrapper<>();

        mmrsTUpdateWrapper.eq("MMRSENT", mmrsT.getMMRSENT());
        mmrsTUpdateWrapper.eq("MMRS001", mmrsT.getMMRS001());
        mmrsTUpdateWrapper.eq("MMRS002", mmrsT.getMMRS002());
        mmrsTUpdateWrapper.eq("MMRS003", mmrsT.getMMRS003());
        mmrsTUpdateWrapper.eq("MMRS004", mmrsT.getMMRS004());
        mmrsTUpdateWrapper.eq("MMRS005", mmrsT.getMMRS005());
        mmrsTUpdateWrapper.eq("MMRS006", mmrsT.getMMRS006());

        mmrsT.delete(mmrsTUpdateWrapper);


    }

    @Transactional
    @Override
    public void delRows(List<MmrsT> mmrsTList) {
        for (MmrsT mmrsT : mmrsTList
        ) {
            UpdateWrapper<MmrsT> mmrsTUpdateWrapper = new UpdateWrapper<>();

            mmrsTUpdateWrapper.eq("MMRSENT", mmrsT.getMMRSENT());
            mmrsTUpdateWrapper.eq("MMRS001", mmrsT.getMMRS001());
            mmrsTUpdateWrapper.eq("MMRS002", mmrsT.getMMRS002());
            mmrsTUpdateWrapper.eq("MMRS003", mmrsT.getMMRS003());
            mmrsTUpdateWrapper.eq("MMRS004", mmrsT.getMMRS004());
            mmrsTUpdateWrapper.eq("MMRS005", mmrsT.getMMRS005());
            mmrsTUpdateWrapper.eq("MMRS006", mmrsT.getMMRS006());

            mmrsT.delete(mmrsTUpdateWrapper);
        }
    }

    @Transactional(readOnly = true)
    @Override
    public Boolean existedTheParams(MmrsT mmrsT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        QueryWrapper<MmrsT> mmrsTQueryWrapper = new QueryWrapper<>();
        mmrsTQueryWrapper.eq("MMRSENT", shiroUser.getDBC());
        mmrsTQueryWrapper.eq("MMRS001", mmrsT.getMMRS001());
        mmrsTQueryWrapper.eq("MMRS002", mmrsT.getMMRS002());
        mmrsTQueryWrapper.eq("MMRS003", mmrsT.getMMRS003());
        mmrsTQueryWrapper.eq("MMRS004", mmrsT.getMMRS004());
        mmrsTQueryWrapper.eq("MMRS005", mmrsT.getMMRS005());
        mmrsTQueryWrapper.eq("MMRS006", mmrsT.getMMRS006());
        return this.baseMapper.selectList(mmrsTQueryWrapper).size() > 0 ? true : false;
    }
}
